import pyglet
from pyglet.gui import PushButton
from typing import List

import resouces
import music


class BaseScene:
    """基础场景类，所有场景应继承此类"""

    def __init__(self, scene_manager):
        self.scene_manager = scene_manager
        self.window = scene_manager.window
        # 绘图批
        self.batch = pyglet.graphics.Batch()
        # 预设分层分组
        self.group0 = pyglet.graphics.Group(order=0)
        self.group1 = pyglet.graphics.Group(order=1)
        self.group2 = pyglet.graphics.Group(order=2)
        self.group3 = pyglet.graphics.Group(order=3)
        self.group4 = pyglet.graphics.Group(order=4)
        # 存储场景中的所有按钮
        self.buttons: List[PushButton] = []
        # 图片资源
        self.pic_resources = resouces.load_pic()
        # 角色立绘
        self.characor_pic_resources = resouces.load_charactor_pic()
        # 音乐？
        self.muchals = {}
        #current_player = None
        self.muc_resources = resouces.load_muc()
        #self.current_pic=0

############################我在考虑把下面的音乐函数移到music.py
    def play_music(self, muc_resources, loop=True):
        self.muchals[muc_resources] = music.my_muchal()
        self.muchals[muc_resources].play_music(self.muc_resources[muc_resources],loop=loop)

    def pause_music(self,muc_resources):
        print("try")
        self.muchals[muc_resources].pause_music()

    def delete_music(self,muc_resources):
        self.pause_music(muc_resources)
        try:
            self.muchals.pop(muc_resources)
        except:
            pass

    def fade_in_picture(self, pic_resource, duration1):
        """实现图片淡入淡出（尽量全屏，处理方法待定）"""
        self.fade_speed = 255/duration1
        self.new_sprite = pyglet.sprite.Sprite(pic_resource, x=self.scene_manager.get_cx(), y=self.scene_manager.get_cy(),batch=self.batch,group=self.group0)
        self.new_sprite.opacity=0
        pyglet.clock.schedule_interval(self.update, 1 / 60.0)

    def update(self, dt):
        """更新要淡入淡出的图片的透明度"""
        if self.new_sprite.opacity <255:
            #self.new_sprite.opacity+=int(255/(duration1/dt))
            self.new_sprite.opacity=int(min(self.new_sprite.opacity + max(self.fade_speed * dt,1), 255))
            #print(self.new_sprite.opacity)
        else:
            self.new_sprite.opacity=255
            pyglet.clock.unschedule(self.update)


    def on_draw(self):
        """子类应重写此方法以实现绘制逻辑"""
        #self.batch.draw()
        pass

    def on_mouse_motion(self, x, y, dx, dy):
        """鼠标移动事件"""
        pass
